/*
 * Copyright 2013 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */
package com.madding.shared.encrypt.cert.bc;

import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.Certificate;

import org.bouncycastle.openssl.PEMWriter;

import com.madding.shared.encrypt.cert.bc.constant.BCConstant;
import com.madding.shared.encrypt.cert.bc.util.KeyStoreUtil;

/**
 * 类CertFileWriter.java的实现描述：证书持久化工具类
 * 
 * @author madding.lip Dec 6, 2013 7:24:53 PM
 */
public class CertFilePersistence implements BCConstant {

    public static final char[] PASSWD     = "hell01234".toCharArray();
    public static final String CRT_SUFFIX = ".crt";
    public static final String KEY_SUFFIX = ".key";
    public static final String P12_SUFFIX = ".p12";

    public static void storeCertFile(String alias, Certificate[] certChain, KeyPair keyPair, String file)
                                                                                                         throws Exception {
        // create alilang crt file
        storeCrtFile(certChain[0], file);

        // create alilang key file
        storeKeyFile(keyPair.getPrivate(), file);

        // create alilang p12 file
        storeP12File(alias, certChain, keyPair, file);
    }

    public static void storeCrtFile(Certificate cert, String file) throws Exception {
        FileOutputStream fos = new FileOutputStream(file + CRT_SUFFIX);
        PEMWriter pemWriter = new PEMWriter(new PrintWriter(fos));
        pemWriter.writeObject(cert);
        pemWriter.flush();
        pemWriter.close();
    }

    public static void storeKeyFile(PrivateKey privKey, String file) throws Exception {
        FileOutputStream fos2 = new FileOutputStream(file + KEY_SUFFIX);
        PEMWriter pemWriter2 = new PEMWriter(new PrintWriter(fos2));
        pemWriter2.writeObject(privKey);
        pemWriter2.flush();
        pemWriter2.close();
    }

    public static void storeKeyFile(KeyPair keyPair, String file) throws Exception {
        FileOutputStream fos2 = new FileOutputStream(file + KEY_SUFFIX);
        PEMWriter pemWriter2 = new PEMWriter(new PrintWriter(fos2));
        pemWriter2.writeObject(keyPair);
        pemWriter2.flush();
        pemWriter2.close();
    }

    public static void storeP12File(String alias, Certificate[] certChain, KeyPair keyPair, String file)
                                                                                                        throws Exception {
        KeyStore store = KeyStoreUtil.getPKCS12KeyStore(alias, certChain, keyPair);
        FileOutputStream fOut = new FileOutputStream(file + P12_SUFFIX);
        store.store(fOut, PASSWD);
        fOut.flush();
        fOut.close();
    }
}
